package com.kyh.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

public final class ResUtil {
	
	private static final Logger log = Logger.getLogger(ResUtil.class);

	public static final int RET_NO = 0; // 返回值--失败

	public static final int RET_YES = 1; // 返回值--成功

	public static final int RET_OVER = 2; // 返回值--超时
	
	private static final BlockingQueue<ResUtil.RetMsg> queue;
	static{
		int poolSize = 800;
		queue = new ArrayBlockingQueue<ResUtil.RetMsg>(poolSize);
		for(int i=0;i<poolSize;i++){
			RetMsg rm = new RetMsg();
			rm.setFromPool(true);//对象池标识
			queue.offer(rm);
		}
	}
	public static RetMsg success(String msg){
		return success(msg, "");
	}
	public static RetMsg success(String msg, Object data){
		RetMsg ret = get();
		ret.setIs(RET_YES);//成功
		ret.setMsg(msg);
		ret.setData(data);
		ret.setSuccess(true);
		return ret;
	}
	public static RetMsg success(String msg, Object data, int allCount){
		RetMsg ret = get();
		ret.setIs(RET_YES);//成功
		ret.setMsg(msg);
		ret.setData(data);
		ret.setAllCount(allCount);
		ret.setSuccess(true);
		return ret;
	}
	
	public static RetMsg fail(String msg){
		RetMsg ret = get();
		ret.setMsg(msg);
		ret.setSuccess(false);
		return ret;
	}
	public static RetMsg fail(String msg, Object data){
		RetMsg ret = get();
		ret.setIs(RET_NO);
		ret.setMsg(msg);
		ret.setData(data);
		ret.setSuccess(false);
		return ret;
	}
	
	public static RetMsg failWithCode(String msg, String code){
		RetMsg ret = get();
		ret.setMsg(msg);
		ret.setSuccess(false);
		ret.setCode(code);
		return ret;
	}
	
	public static void fail(HttpServletResponse response, String msg){
		RetMsg ret = fail(msg);
		ret.setSuccess(false);
		ret.write(response);
	}
	
	public static void failWithCode(HttpServletResponse response, String msg, String code){
		RetMsg ret = failWithCode(msg, code);
		ret.setSuccess(false);
		ret.write(response);
	}
	
	public static RetMsg over(){
		return over("请求超时");
	}
	public static RetMsg over(String msg){
		RetMsg ret = get();
		ret.setIs(RET_OVER);//超时
		ret.setMsg(msg);
		ret.setSuccess(false);
		return ret;
	}
	private static RetMsg get(){
		RetMsg ret = queue.poll();
		if(ret == null){
			ret = new RetMsg();
		}
		ret.setTime(System.currentTimeMillis());
		return ret;
	}
	
	public static class RetMsg{
		private int is = RET_NO;
	    private Object data; // modelClass或者modelList
	    private String msg;
	    private int allCount; // 总条数
	    private String code;
	    private long time;//毫秒级时间
	    private String token; //uuid
	    private boolean success;
	    
	    private boolean fromPool=false;//对象池专用，不会进行序列化。
		
		public RetMsg(){}
		
		public void write(HttpServletResponse response){
			writeDataWithlog(response, JSON.toJSONString(this));
		}
		
		public void writeWithoutlog(HttpServletResponse response){
			writeDataWithoutlog(response, JSON.toJSONString(this));
		}
		
		private void writeDataWithlog(HttpServletResponse response, String data){
			System.out.println(data);
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = null;
			try{
				pw = response.getWriter();
				pw.write(data);
				pw.flush();
			} catch (IOException e) {
				log.error("输出异常",e);
			} finally {
				if(pw != null){
					pw.close();
				}
				if(this.isFromPool()){
					this.clear();
					queue.offer(this);
				}
			}
		}
		
		private void writeDataWithoutlog(HttpServletResponse response, String data){
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = null;
			try{
				pw = response.getWriter();
				pw.write(data);
				pw.flush();
			} catch (IOException e) {
				log.error("输出异常",e);
			} finally {
				if(pw != null){
					pw.close();
				}
				if(this.isFromPool()){
					this.clear();
					queue.offer(this);
				}
			}
		}
		
		
		private void clear(){
			is = 0;
			msg = "";
			data = null;
			allCount = 0;
			code = "";
			token = "";
			time = 0;
			fromPool = true;
		}
	    
		
		public long getTime() {
			return time;
		}
		public void setTime(long time) {
			this.time = time;
		}
		@JSONField(serialize=false)
	    public boolean isFromPool() {
	    	return fromPool;
	    }
	    public void setFromPool(boolean fromPool) {
	    	this.fromPool = fromPool;
	    }
		public int getIs() {
			return is;
		}
		public void setIs(int is) {
			this.is = is;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public Object getData() {
			return data;
		}
		public void setData(Object data) {
			this.data = data;
		}
		public int getAllCount() {
			return allCount;
		}
		public void setAllCount(int allCount) {
			this.allCount = allCount;
		}
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getToken() {
			return token;
		}
		public void setToken(String token) {
			this.token = token;
		}
		public boolean isSuccess() {
			return success;
		}
		public void setSuccess(boolean success) {
			this.success = success;
		}
	}
	
}
